home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 2 / Macwelt DVD 2.cdr / System / Internet-Utilities / macosx / News Mac 1.1.dmg / Parser.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-04-08  |  6.9 KB  |  235 lines

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.net.URL;
  4. import java.util.StringTokenizer;
  5. import java.util.Vector;
  6.  
  7. public class Parser {
  8.    protected Channel channel = new Channel();
  9.    protected Vector html = new Vector(500, 100);
  10.    private int headlinesFound = 0;
  11.    private String headlines = "";
  12.  
  13.    public Parser() {
  14.    }
  15.  
  16.    public Parser(Channel theChannel) throws Exception {
  17.       try {
  18.          this.channel = theChannel;
  19.          this.html = this.getPage(this.channel.getSource(), false);
  20.       } catch (Exception var3) {
  21.          throw new Exception("Could not read the HTML file.");
  22.       }
  23.    }
  24.  
  25.    public Parser(String theChannel, String htmlFile) throws Exception {
  26.       try {
  27.          this.channel = (Channel)IOUtil.openObject(theChannel);
  28.       } catch (Exception var5) {
  29.          throw new Exception("Could not open channel " + theChannel);
  30.       }
  31.  
  32.       try {
  33.          this.html = this.getPage(htmlFile, false);
  34.       } catch (Exception var4) {
  35.          throw new Exception("Could not open HTML file " + htmlFile);
  36.       }
  37.    }
  38.  
  39.    protected void saveToCache(String fileName) throws Exception {
  40.       String temp = "";
  41.  
  42.       for(int i = 0; i < this.html.size(); ++i) {
  43.          temp = temp + (String)this.html.elementAt(i);
  44.       }
  45.  
  46.       try {
  47.          IOUtil.saveTextFile(fileName, temp, false);
  48.       } catch (Exception var5) {
  49.          throw new Exception("Parser: Error saving cache file " + fileName + "\n" + var5);
  50.       }
  51.    }
  52.  
  53.    private String filterTags(String source) {
  54.       String temp = "";
  55.       boolean ignore = false;
  56.  
  57.       for(int i = 0; i < source.length(); ++i) {
  58.          if (!ignore & source.charAt(i) != '<' & source.charAt(i) != '>') {
  59.             temp = temp + source.charAt(i);
  60.          }
  61.  
  62.          if (source.charAt(i) == '<') {
  63.             ignore = true;
  64.          } else if (source.charAt(i) == '>') {
  65.             ignore = false;
  66.          }
  67.       }
  68.  
  69.       temp = this.charConvert(temp);
  70.       return temp;
  71.    }
  72.  
  73.    protected String charConvert(String source) {
  74.       String[] charList = new String[]{"“", "#146;", "”", "&", " ", """, "’", "–", "“", "”", "£", "~", "¨", "©", "—", "<", ">", "£", "'"};
  75.       String[] replaceList = new String[]{"\"", "'", "\"", "&", " ", "\"", "'", "-", "\"", "\"", "¬£", "~", "\"", "¬©", " ", "<", ">", "¬£", "'"};
  76.  
  77.       for(int i = 0; i < source.length(); ++i) {
  78.          for(int n = 0; n < charList.length; ++n) {
  79.             int wordLength = charList[n].length();
  80.             if (i + wordLength < source.length() && source.substring(i, i + wordLength).equalsIgnoreCase(charList[n])) {
  81.                source = source.substring(0, i) + replaceList[n] + source.substring(i + wordLength, source.length());
  82.             }
  83.          }
  84.       }
  85.  
  86.       return source;
  87.    }
  88.  
  89.    protected String[] findHeadlines() throws Exception {
  90.       int number = this.channel.getHeadlineCount();
  91.       String[] returnString = new String[number];
  92.       boolean headline = false;
  93.       String htmlData = "";
  94.       String openingMarker = this.channel.getOpeningMarker().toLowerCase();
  95.       String closingMarker = this.channel.getClosingMarker().toLowerCase();
  96.       String startMarker = this.channel.getStartMarker().toLowerCase();
  97.       String endMarker = this.channel.getEndMarker().toLowerCase();
  98.       int n = 0;
  99.       int startLength = startMarker.length();
  100.       int endLength = endMarker.length();
  101.       int openingMarkerLength = openingMarker.length();
  102.       int closingMarkerLength = closingMarker.length();
  103.       int startingPosition = 0;
  104.       boolean headlineSection = false;
  105.       String carryOver = "";
  106.  
  107.       for(int lineCount = 0; lineCount < this.html.size(); ++lineCount) {
  108.          String thisLine = ((String)this.html.elementAt(lineCount)).trim();
  109.          if (n < number) {
  110.             for(int i = 0; i < thisLine.length(); ++i) {
  111.                if (n < number) {
  112.                   try {
  113.                      if (i + startLength <= thisLine.length() & !startMarker.equals("") && thisLine.substring(i, i + startLength).equalsIgnoreCase(startMarker)) {
  114.                         headlineSection = true;
  115.                         i += startLength;
  116.                      }
  117.                   } catch (Exception var25) {
  118.                      throw new Exception("Parser: Failed locating start marker.");
  119.                   }
  120.  
  121.                   try {
  122.                      if (i + endLength <= thisLine.length() & !endMarker.equals("") && thisLine.substring(i, i + endLength).equalsIgnoreCase(endMarker)) {
  123.                         headlineSection = false;
  124.                      }
  125.                   } catch (Exception var24) {
  126.                      throw new Exception("Parser: Failed locating end marker.\n" + var24);
  127.                   }
  128.  
  129.                   if (startMarker.equals("")) {
  130.                      headlineSection = true;
  131.                   }
  132.  
  133.                   try {
  134.                      if (i + openingMarkerLength <= thisLine.length() & headlineSection && thisLine.substring(i, i + openingMarkerLength).equalsIgnoreCase(openingMarker)) {
  135.                         startingPosition = i + openingMarkerLength;
  136.                         headline = true;
  137.                      }
  138.                   } catch (Exception var23) {
  139.                      throw new Exception("Parser: Failed locating opening marker.");
  140.                   }
  141.  
  142.                   try {
  143.                      if (i + closingMarkerLength <= thisLine.length() & headlineSection && thisLine.substring(i, i + closingMarkerLength).equalsIgnoreCase(closingMarker)) {
  144.                         if (!carryOver.equals("")) {
  145.                            try {
  146.                               returnString[n] = this.filterTags(carryOver + thisLine.substring(0, i)).trim();
  147.                               ++n;
  148.                            } catch (Exception var22) {
  149.                               throw new Exception("Parser: Processing carry over headline.");
  150.                            }
  151.                         } else {
  152.                            try {
  153.                               if (startingPosition < i & i < thisLine.length()) {
  154.                                  String theString = thisLine.substring(startingPosition, i);
  155.                                  String temp = this.filterTags(theString);
  156.                                  if (!this.filterTags(temp).equals("")) {
  157.                                     returnString[n] = temp;
  158.                                     ++n;
  159.                                  }
  160.                               }
  161.                            } catch (Exception var27) {
  162.                               throw new Exception("Parser: Processing normal headline failed.");
  163.                            }
  164.                         }
  165.  
  166.                         carryOver = "";
  167.                         startingPosition = 0;
  168.                         headline = false;
  169.                      }
  170.                   } catch (Exception var28) {
  171.                      throw new Exception("Parser: Failed processing closing marker.");
  172.                   }
  173.  
  174.                   try {
  175.                      if (i == thisLine.length() - 1 & headline && startingPosition < i) {
  176.                         if (carryOver.equals("")) {
  177.                            carryOver = carryOver + thisLine.substring(startingPosition, i + 1) + " ";
  178.                         } else {
  179.                            carryOver = carryOver + thisLine;
  180.                         }
  181.                      }
  182.                   } catch (Exception var26) {
  183.                      throw new Exception("Parsing error: Failed managing an overflow line.");
  184.                   }
  185.                }
  186.             }
  187.          }
  188.       }
  189.  
  190.       return returnString;
  191.    }
  192.  
  193.    protected void getHTML() throws Exception {
  194.       try {
  195.          this.html = this.getPage(this.channel.getSource(), false);
  196.       } catch (Exception var2) {
  197.          throw new Exception("Error downloading " + this.channel.getName());
  198.       }
  199.    }
  200.  
  201.    protected Vector getPage(String theURL, boolean parse) throws Exception {
  202.       try {
  203.          URL theSite = new URL(theURL);
  204.          BufferedReader in = new BufferedReader(new InputStreamReader(theSite.openStream()));
  205.          Vector tempVector = new Vector(200, 10);
  206.          int p = 0;
  207.          if (parse) {
  208.             throw new Exception("This function is not yet available!");
  209.          } else {
  210.             String inputLine;
  211.             while((inputLine = in.readLine()) != null) {
  212.                tempVector.add(inputLine);
  213.             }
  214.  
  215.             in.close();
  216.             return tempVector;
  217.          }
  218.       } catch (Exception var8) {
  219.          throw new Exception("Could not get channel from " + theURL);
  220.       }
  221.    }
  222.  
  223.    protected String[] getHeadlines() {
  224.       StringTokenizer st = new StringTokenizer(this.headlines, "~");
  225.       int n = 0;
  226.  
  227.       String[] temp;
  228.       for(temp = new String[this.channel.getHeadlineCount()]; n < this.channel.getHeadlineCount() & st.hasMoreTokens(); ++n) {
  229.          temp[n] = st.nextToken();
  230.       }
  231.  
  232.       return temp;
  233.    }
  234. }
  235.